//
// StoreNuke.Net -  http://www.storenuke.net
// Copyright (c) 2005
// by Robert J Collins ( info@storenuke.net ) of WillowTree Software, Inc. ( http://www.willowtreesoftware.com )
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
// documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
// to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions 
// of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
// CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
// DEALINGS IN THE SOFTWARE.
//

using System.Collections.Specialized;
using System.IO;
using System.Net;
using System.Text;
using System.Web;

public class AuthNetPaymentAuth 
{ 
	public string[] RequestAuth(string x_First_Name, string x_Last_Name, string x_Company, string x_Address, string x_City, string x_State, string x_Zip, string x_Phone, string x_Amount, string x_Card_Num, string x_Card_Code, string x_Exp_Month, string x_Exp_Year, string x_Recurring_Billing, string x_invoice_num, string x_GatewayURL, string x_Version, string x_IsTest, string x_Login, string x_Password, string x_Type)
	{
		string url = x_GatewayURL;
		NameValueCollection NVCol = new NameValueCollection();
		NVCol.Add("x_Version", x_Version);
		NVCol.Add("x_Delim_Data", "True");
		NVCol.Add("x_Login", x_Login);
		NVCol.Add("x_Password", x_Password);
		NVCol.Add("x_Test_Request", x_IsTest);
		NVCol.Add("x_Delim_Char", "~");
		NVCol.Add("x_Encap_Char", "'");

		NVCol.Add("x_First_Name", x_First_Name);
		NVCol.Add("x_Last_Name", x_Last_Name);
		NVCol.Add("x_Company", x_Company);
		NVCol.Add("x_Address", x_Address);
		NVCol.Add("x_City", x_City);
		NVCol.Add("x_State", x_State);
		NVCol.Add("x_Zip", x_Zip);
		NVCol.Add("x_Country", "US");
		NVCol.Add("x_Phone", x_Phone);
		NVCol.Add("x_invoice_num", x_invoice_num);

		NVCol.Add("x_Amount", x_Amount);
		NVCol.Add("x_Method", "CC");
		NVCol.Add("x_Card_Num", x_Card_Num);
		NVCol.Add("x_Card_Code", x_Card_Code);
		NVCol.Add("x_Exp_Date", x_Exp_Month + "/" + x_Exp_Year);
		NVCol.Add("x_Recurring_Billing", x_Recurring_Billing);
		NVCol.Add("x_Type", x_Type);

		string[] MySplitLine = null;
		try
		{
			Encoding enc = Encoding.GetEncoding(1252);
			StreamReader loResponseStream = new StreamReader(PostEx(url, NVCol).GetResponseStream(), enc);
			string lcHtml = loResponseStream.ReadToEnd();
			loResponseStream.Close();
			MySplitLine = Microsoft.VisualBasic.Strings.Split(lcHtml.TrimStart("'".ToCharArray()), "'~'", -1, Microsoft.VisualBasic.CompareMethod.Binary);
		}
		catch
		{
			//Return error
			MySplitLine = Microsoft.VisualBasic.Strings.Split("2|0|0|No Connection Available", "|", -1, Microsoft.VisualBasic.CompareMethod.Binary);
		}
		return MySplitLine;
	}

	public WebResponse PostEx(string url, NameValueCollection values)
	{
		WebRequest request = null;
		StringBuilder builder = null;
		string[] keys = null;
		//INSTANT C# NOTE: Commented this declaration since looping variables in 'foreach' loops are declared in the 'foreach' header in C#
		//		string key = null;
		Stream stream = null;
		byte[] bytes = null;
		try
		{
			request = WebRequest.Create(url);
			request.Method = "POST";
			request.ContentType = "application/x-www-form-urlencoded";

			if (values.Count == 0)
			{
				request.ContentLength = 0;
			}
			else
			{
				builder = new StringBuilder();
				keys = values.AllKeys;
				foreach (string key in keys)
				{
					if (builder.Length > 0)
					{
						builder.Append("&");
					}
					builder.Append(HttpUtility.UrlEncode(key));
					builder.Append("=");
					builder.Append(HttpUtility.UrlEncode(values[key]));
				}
				bytes = Encoding.UTF8.GetBytes(builder.ToString());
				request.ContentLength = bytes.Length;
				stream = request.GetRequestStream();
				stream.Write(bytes, 0, bytes.Length);
				stream.Close();
			}
			return request.GetResponse();
		}
		catch
		{
			return null;
		}
	}
}